package org.ar.gameserver.login.server;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.ar.gameserver.common.exception.GameServerException;
import org.ar.gameserver.common.socket.ReceivedMessageType;
import org.ar.gameserver.common.util.Message;
import org.ar.gameserver.login.service.UserLoginService;
import org.ar.gameserver.login.service.bean.User;
import org.ar.gameserver.login.service.bean.UserRole;

import flex.messaging.io.amf.ASObject;

public class LoginServerHandler extends IoHandlerAdapter {

	private static Map<String, IoSession> sessionMap = new ConcurrentHashMap<String, IoSession>();
	
	private UserLoginService userLoginService = new UserLoginService();
	
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		super.exceptionCaught(session, cause);
	}

	public void messageReceived(IoSession session, Object message)
			throws Exception {
		Message<User> receivedMessage = new Message<User>((ASObject)message);
		Message<List<UserRole>> sendMessage = new Message<List<UserRole>>();
		try {
			List<UserRole> returnList = null;
			String messageHead = receivedMessage.getMessageHead();
			User user = receivedMessage.getReturnObject();
			Object[] objects = {user.getUser_name(),user.getUser_password()};
			
			if(ReceivedMessageType.LOGIN_SERVER_TYPE_1.equals(messageHead)) {
				userLoginService.userLogin(objects);
			}
			
			sendMessage.setMessageHead(messageHead);
			sendMessage.setReturnObject(returnList);
			
			session.write(sendMessage.getDataPackage());
		} catch (GameServerException e) {
			sendMessage.setException(e.toString());
		}
		
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		super.messageSent(session, message);
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		super.sessionCreated(session);
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		super.sessionIdle(session, status);
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
	}
	
}
